package leo.weixin.test;


import com.alibaba.fastjson.JSON;
import com.baidu.aip.ocr.AipOcr;
import leo.weixin.WeixinApplication;
import leo.weixin.config.AppConfig;
import leo.weixin.entity.material.Article;
import leo.weixin.entity.menu.*;
import leo.weixin.service.IWxService;
import leo.weixin.service.impl.WxServiceImpl;
import leo.weixin.util.RestTemplateUtil;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class Main {
    @Autowired
    IWxService iWxService ;
    @Test
    public void testStringBuilder() {
        StringBuilder sb = new StringBuilder("aaa");
        System.out.println(sb.toString());
    }

    @Test
    public void testButton() {
        Button button = new Button();
        button.add(new SubButton("主菜单一")
                //.add(EventButton.createByNameKeyType("扫码带提示","SCANCODE_WAITMSG", EventButton.EventButtonType.SCANCODE_WAITMSG))
                //.add(EventButton.createByNameKeyType("扫码推事件","SCANCODE_PUSH", EventButton.EventButtonType.SCANCODE_PUSH))
                // 拍照或者相册发图 PIC_PHOTO_OR_ALBUM  recognition
                .add(EventButton.createByNameKeyType("图像识别", "event_imageRecognition", EventButton.EventButtonType.PIC_PHOTO_OR_ALBUM))
                .add(EventButton.createByNameKeyType("微信相册发图", "PIC_WEIXIN", EventButton.EventButtonType.PIC_WEIXIN))
                .add(EventButton.createByNameKeyType("系统拍照发图", "PIC_SYSPHOTO", EventButton.EventButtonType.PIC_SYSPHOTO)))
                .add(new SubButton("主菜单二")
                        .add(EventButton.createLocationButton("发送位置", "click_location"))
                        .add(new ViewButton("QQ音乐2", "https://y.qq.com/"))
                        .add(new ViewButton("打开小程序", "https://y.qq.com/")) //todo  修改为小程序
                        .add(EventButton.createClickButton("支持我", "click_support")));
        //.add(MediaButton.createNewsMediaButton("图文消息按钮","111"))  // 好像要填写正确的 mediaid才可以
        //.add(MediaButton.createTextMediaButton("文本消息按钮","222"))));

        String json = JSON.toJSONString(button);
        System.out.println(json);
    }


    /**
     * {
     * "type": "pic_photo_or_album",
     * "name": "拍照或者相册发图",
     * "key": "rselfmenu_1_1",
     * "sub_button": [ ]
     * },
     * {
     * "type": "pic_weixin",
     * "name": "微信相册发图",
     * "key": "rselfmenu_1_2",
     * "sub_button": [ ]
     * }
     */
    @Test
    public void testKeyEventButton() {
        Button button = new Button();
        button.add(new SubButton("扫码")
                .add(EventButton.createByNameKeyType("扫码带提示", "rselfmenu_0_0", EventButton.EventButtonType.SCANCODE_WAITMSG))
                .add(EventButton.createByNameKeyType("扫码推事件", "rselfmenu_0_1", EventButton.EventButtonType.SCANCODE_PUSH)))
                .add(new SubButton("发图")
                        .add(EventButton.createByNameKeyType("拍照或者相册发图", "rselfmenu_1_1", EventButton.EventButtonType.PIC_PHOTO_OR_ALBUM))
                        .add(EventButton.createByNameKeyType("微信相册发图", "pic_sysphoto", EventButton.EventButtonType.PIC_WEIXIN))
                );
        EventButton eventButton = EventButton.createClickButton("点击", "k2");
        //System.out.println(JSON.toJSONString(button));
        System.out.println(JSON.toJSONString(eventButton));
    }

    @Test
    public void testImageRec() {
        //设置APPID/AK/SK
         String APP_ID = "17626539";
         String API_KEY = "TRHoLWVbeLInXLyeWMpSG7EM";
         String SECRET_KEY = "ScvYN2fPqxUGNhexPcO8dpMU1VhggTMO";


        AipOcr client = new AipOcr(APP_ID,API_KEY,SECRET_KEY);
        /*
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
        */
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("detect_language", "true");

        String url = "http://mmbiz.qpic.cn/mmbiz_jpg/iatcfA8XJic6X4q61vd86cOeiaEwrCTf0O2TF9iaicTCARPlcRKfBAAK8YqqUyeqS8ySqkwWwZ3cRlCpKuno6icYoPRQ/0";
/*
        // 参数为本地路径
        String image = "test.jpg";
        JSONObject res = client.webImage(image, options);
        System.out.println(res.toString(2));

        // 参数为二进制数组
        byte[] file = readFile("test.jpg");
        res = client.webImage(file, options);
        System.out.println(res.toString(2));
*/
        // 网络图片文字识别, 图片参数为远程url图片
        JSONObject res = client.webImageUrl(url, options);
        System.out.println(res.toString(2));

    }

    @Test
    public void testCreateTempFile() throws IOException {
        File f = File.createTempFile("prefix",".suffix");  //prefix6339985936674881165.suffix
        //f = File.createTempFile("prefix",".suffix",new File("E:/wmx"));  //不存在不能
        System.out.println(f.getAbsolutePath());
        System.out.println(f.getName());
    }

    @Test
    public void testArticleBuilder() {
        Article article = Article.builder()
                .title("title")
                .content("content")
                .contentSourceUrl("contentUrl")
                .showCoverPic(false)
                .thumbMediaId("mid")
                .build()
                .setAuthor("author")
                .setDigest("digest")
                .setOnlyFansCanComment(true)
                .setNeedOpenComment(true);
        System.out.println(JSON.toJSONString(article,true));
    }

}
